﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ZhongheCms.Models
{
    public partial class GswTblSno
    {
        public static int GetSno(string keycode)
        {
            int sno;
            var tblSno = SingleOrDefault("where keycode=@0", keycode);
            if (tblSno == null)
            {
                sno = 1;
                var objSno = new GswTblSno
                {
                    CurYear = DateTime.Today.Year,
                    Sno = 1,
                    KeyCode = keycode,
                    CurMonth = 0

                };
                objSno.Insert();

            }

            else
            {
                if (tblSno.CurYear == DateTime.Today.Year)
                    sno = tblSno.Sno;
                else
                {
                    sno = 1;
                    Update("set curmonth=0, sno=@0,curyear=@1 where keycode=@2", sno, DateTime.Today.Year, keycode);
                }
            }

            return sno;
        }

        public static int GetSnoByMonth(string keycode)
        {
            int sno;
            var tblSno = SingleOrDefault("where keycode=@0", keycode);
            if (tblSno == null)
            {
                sno = 1;
                var objSno = new GswTblSno
                {
                    CurYear = DateTime.Today.Year,
                    CurMonth = DateTime.Today.Month,
                    Sno = 1,
                    KeyCode = keycode,
                    CreateDate = DateTime.Today.ToShortDateString()
                };
                objSno.Insert();

            }

            else
            {
                if (tblSno.CurYear == DateTime.Today.Year && tblSno.CurMonth == DateTime.Today.Month)
                    sno = tblSno.Sno;
                else
                {
                    sno = 1;
                    Update("set sno=@0,curyear=@1,curmonth=@2 where keycode=@3", sno, DateTime.Today.Year, DateTime.Today.Month, keycode);
                }
            }

            return sno;
        }
    }
}